<script setup lang="ts">
import { appEvents } from '/@/utils/events';

const props = defineProps<{
  item: FlowPathTemplateApi.AttributeExtendDto;
}>();

/**
 * 个数化数字
 * @param val 原始数字
 */
function formatter(val: string) {
  if (
    val.indexOf('.') >= 0 &&
    props.item.attributePrecision &&
    val.slice(val.indexOf('.') + 1).length > props.item.attributePrecision
  ) {
    //小数个数
    const dotLength = props.item.attributePrecision;
    const newValue = val.substring(0, val.indexOf('.') + 1 + dotLength);
    console.log(newValue);
    return newValue;
  }
  return val;
}
</script>
<!--5-数字输入框-->
<template>
  <!--只读 -->
  <template v-if="item.readonlyAble === 1">
    <nut-cell
      :title="item.attributeName + (item.attributeUnit ? `(${item.attributeUnit})` : '')"
      :desc="item.attributeValue"
    />
  </template>
  <!--编辑 -->
  <template v-else>
    <nut-form-item
      :label="item.attributeName + (item.attributeUnit ? `(${item.attributeUnit})` : '')"
      :required="item.attributeRequired === 1"
      :label-width="120"
    >
      <nut-input
        :placeholder="item.tips ? item.tips : '请输入数字'"
        input-width="50"
        type="digit"
        class="approve-edit-input-number"
        @blur="
          () => {
            if (item.isCondition) {
              appEvents.trigger('conditionAttributeChanged');
            }
          }
        "
        input-align="right"
        v-model="item.attributeValue"
        :formatter="formatter"
        format-trigger="onChange"
        :border="false"
      />
    </nut-form-item>
  </template>
</template>
<style lang="scss">
.approve-edit-input-number {
  //数字输入框
  .nut-input-number {
    &__text--input {
      background-color: #eee !important;
    }
  }
}
</style>
